<template>
	<view class="diy_edit page_adoption_information" id="adoption_information_edit">
		<view class='warp'>
			<view class='container'>
				<view class='row'>
					<view v-if="$check_field('set','pet_name') || $check_field('add','pet_name') || $check_field('get','pet_name')" class="col-12 col-md-6">
						<view class="diy_title">
							<text>宠物名称:
							</text>
						</view>
						<!-- 文本 -->
						<view class="diy_field diy_text">
							<input type="text" id="form_pet_name" v-model="form['pet_name']" placeholder="请输入宠物名称" v-if="(form['pet_name'] && $check_field('set','pet_name')) || (!form['pet_name'] && $check_field('add','pet_name'))" :disabled="disabledObj['pet_name_isDisabled']" />
							<text v-else-if="$check_field('get','pet_name')">{{ form['pet_name'] }}</text>
						</view>
					</view>
					<view v-if="$check_field('set','pet_species') || $check_field('add','pet_species') || $check_field('get','pet_species')" class="col-12 col-md-6">
						<view class="diy_title">
							<text>宠物种类:
							</text>
						</view>
						<!-- 文本 -->
						<view class="diy_field diy_text">
							<input type="text" id="form_pet_species" v-model="form['pet_species']" placeholder="请输入宠物种类" v-if="(form['pet_species'] && $check_field('set','pet_species')) || (!form['pet_species'] && $check_field('add','pet_species'))" :disabled="disabledObj['pet_species_isDisabled']" />
							<text v-else-if="$check_field('get','pet_species')">{{ form['pet_species'] }}</text>
						</view>
					</view>
					<view v-if="$check_field('set','user') || $check_field('add','user') || $check_field('get','user')" class="col-12 col-md-6">
						<view class="diy_title">
							<text>用户:
							</text>
						</view>
						<view class="diy_field diy_down">
							<select id="form_user" :disabled="disabledObj['user_isDisabled']" v-model="form['user']" v-if="(form['user'] && $check_field('set','user')) || (!form['user'] && $check_field('add','user'))" >
								<option v-for="o in list_user_user" :value="o['user_id']">
									{{o['nickname'] + '-' + o['username']}}
								</option>
							</select>
							<text v-else-if="$check_field('get','user')">{{ form['user'] }}</text>
						</view>
					</view>
					<view v-if="$check_field('set','full_name') || $check_field('add','full_name') || $check_field('get','full_name')" class="col-12 col-md-6">
						<view class="diy_title">
							<text>姓名:
							</text>
						</view>
						<!-- 文本 -->
						<view class="diy_field diy_text">
							<input type="text" id="form_full_name" v-model="form['full_name']" placeholder="请输入姓名" v-if="(form['full_name'] && $check_field('set','full_name')) || (!form['full_name'] && $check_field('add','full_name'))" :disabled="disabledObj['full_name_isDisabled']" />
							<text v-else-if="$check_field('get','full_name')">{{ form['full_name'] }}</text>
						</view>
					</view>
					<view v-if="$check_field('set','age') || $check_field('add','age') || $check_field('get','age')" class="col-12 col-md-6">
						<view class="diy_title">
							<text>年龄:
							</text>
						</view>
						<!-- 文本 -->
						<view class="diy_field diy_text">
							<input type="text" id="form_age" v-model="form['age']" placeholder="请输入年龄" v-if="(form['age'] && $check_field('set','age')) || (!form['age'] && $check_field('add','age'))" :disabled="disabledObj['age_isDisabled']" />
							<text v-else-if="$check_field('get','age')">{{ form['age'] }}</text>
						</view>
					</view>
					<view v-if="$check_field('set','contact_number') || $check_field('add','contact_number') || $check_field('get','contact_number')" class="col-12 col-md-6">
						<view class="diy_title">
							<text>联系电话:
							</text>
						</view>
						<!-- 文本 -->
						<view class="diy_field diy_text">
							<input type="text" id="form_contact_number" v-model="form['contact_number']" placeholder="请输入联系电话" v-if="(form['contact_number'] && $check_field('set','contact_number')) || (!form['contact_number'] && $check_field('add','contact_number'))" :disabled="disabledObj['contact_number_isDisabled']" />
							<text v-else-if="$check_field('get','contact_number')">{{ form['contact_number'] }}</text>
						</view>
					</view>
					<view v-if="$check_field('set','adoption_address') || $check_field('add','adoption_address') || $check_field('get','adoption_address')" class="col-12 col-md-6">
						<view class="diy_title">
							<text>领养地址:
							</text>
						</view>
						<!-- 文本 -->
						<view class="diy_field diy_text">
							<input type="text" id="form_adoption_address" v-model="form['adoption_address']" placeholder="请输入领养地址" v-if="(form['adoption_address'] && $check_field('set','adoption_address')) || (!form['adoption_address'] && $check_field('add','adoption_address'))" :disabled="disabledObj['adoption_address_isDisabled']" />
							<text v-else-if="$check_field('get','adoption_address')">{{ form['adoption_address'] }}</text>
						</view>
					</view>
					<view v-if="$check_field('set','audit_status') || $check_field('add','audit_status') || $check_field('get','audit_status')" class="col-12 col-md-6">
						<view class="diy_title">
							<text>审核状态:
							</text>
						</view>
						<!-- 选项 -->
						<view class="diy_field diy_down">
							<select id="form_audit_status" v-model="form['audit_status']" v-if="(form['audit_status'] && $check_field('set','audit_status')) || (!form['audit_status'] && $check_field('add','audit_status'))" >
								<option v-for="o in list_audit_status" :value="o">
									{{o}}
								</option>
							</select>
							<text v-else-if="$check_field('get','audit_status')">{{ form['audit_status'] }}</text>
						</view>
					</view>
					<view v-if="$check_field('set','review_reply') || $check_field('add','review_reply') || $check_field('get','review_reply')" class="col-12 col-md-6">
						<view class="diy_title">
							<text>审核回复:
							</text>
						</view>
						<!-- 多文本 -->
						<view class="diy_field diy_desc">
							<textarea id="form_review_reply" v-model="form['review_reply']" v-if="(form['review_reply'] && $check_field('set','review_reply')) || (!form['review_reply'] && $check_field('add','review_reply'))" :disabled="disabledObj['review_reply_isDisabled']"/>
							<text v-else-if="$check_field('get','review_reply')">{{ form['review_reply'] }}</text>
						</view>
					</view>
				</view>
				<view class="row">
					<view class="col-12">
						<view class="btn_box">
							<button class="btn_submit" @click="submit()">提交</button>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import mixin from "@/mixins/page.js";
export default {
	mixins:[mixin],
	components:{},
	data(){
		return{
			url_get_obj:"~/api/adoption_information/get_obj?",
			url_add:"~/api/adoption_information/add?",
			url_set:"~/api/adoption_information/set?",

			// 登录权限
			oauth: {
				"signIn": true,
				"user_group": []
			},

			// 查询条件
			query: {
				"pet_name": "",
				"pet_species": "",
				"user": 0,
				"full_name": "",
				"age": "",
				"contact_number": "",
				"adoption_address": "",
				"audit_status": "",
				"review_reply": "",
				"adoption_information_id": 0
			},

			obj: {
				"pet_name":'', // 宠物名称
				"pet_species":'', // 宠物种类
				"user": 0, // 用户
				"full_name":'', // 姓名
				"age":'', // 年龄
				"contact_number":'', // 联系电话
				"adoption_address":'', // 领养地址
				"audit_status":'', // 审核状态
				"review_reply":'', // 审核回复
				"adoption_information_id": 0,

			},

			// 表单字段
			form: {
				"pet_name":'', // 宠物名称
				"pet_species":'', // 宠物种类
				"user": 0, // 用户
				"full_name":'', // 姓名
				"age":'', // 年龄
				"contact_number":'', // 联系电话
				"adoption_address":'', // 领养地址
				"audit_status":'', // 审核状态
				"review_reply":'', // 审核回复
				"adoption_information_id": 0,
			},
			disabledObj:{
				"pet_name_isDisabled": false,
				"pet_species_isDisabled": false,
				"user_isDisabled": false,
				"full_name_isDisabled": false,
				"age_isDisabled": false,
				"contact_number_isDisabled": false,
				"adoption_address_isDisabled": false,
				"audit_status_isDisabled": false,
				"review_reply_isDisabled": false,
			},
			// 用户列表
			list_user_user: [],
			list_audit_status: ['已通过','未通过'],

			field:"adoption_information_id",
			table_key:"adoption_information",
		}
	},
	methods: {
		/**
		 * 获取普通用户用户列表
		 */
		async get_list_user_user() {
			// if(this.user_group !== "管理员" && this.form["user"] === 0) {
			//     this.form["user"] = this.user.user_id;
			// }
			var json = await this.$get("~/api/user/get_list?user_group=普通用户");
			if(json.result && json.result.list){
				this.list_user_user = json.result.list;
			}
			else if(json.error){
				console.error(json.error);
			}
		},
		async get_user_session_user(){
			var _this = this;
			var json = await this.$get("~/api/user_group/get_obj?name=普通用户");
			if(json.result && json.result.obj){
				var source_table = json.result.obj.source_table;
				var user_id = _this.$store.state.user.user_id;
				if (user_id){
					var url = "~/api/"+source_table+"/get_obj?"
					this.$get(url, {"user_id":_this.$store.state.user.user_id}, function(res) {
						if (res.result && res.result.obj) {
							var arr = []
							for (let key in res.result.obj) {
								arr.push(key)
							}
							var arrForm = []
							for (let key in _this.form) {
								arrForm.push(key)
							}
							_this.form["user"] = user_id
							_this.disabledObj['user' + '_isDisabled'] = true
							for (var i=0;i<arr.length;i++){
								for (var j=0;j<arrForm.length;j++){
									if (arr[i]===arrForm[j]){
										if (arr[i]!=="user") {
											_this.form[arrForm[j]] = res.result.obj[arr[i]]
											_this.disabledObj[arrForm[j] + '_isDisabled'] = true
											break;
										}
									}
								}
							}
						}
					});
				}
			}
			else if(json.error){
				console.error(json.error);
			}
		},
		change_file(key_name){
			var _self = this;
			// 选择图像方法
			uni.chooseFile({
				count: 1,
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album'], //从相册选择
				success: function(res) {
					const tempFilePaths = res.tempFilePaths;
					const uploadTask = uni.uploadFile({
						url: _self.$fullUrl('/api/adoption_information/upload?'),
						filePath: tempFilePaths[0],
						name: 'file',
						formData: {
							'i_want_to_customize': 'test'
						},
						header: {
							'x-auth-token': _self.$store.state.user.token
						},
						success: function(uploadFileRes) {
							var filename = JSON.parse(uploadFileRes.data).result.url
							_self.form[key_name] = filename
						}
					});

					uploadTask.onProgressUpdate(function(res) {
						_self.percent = res.progress;
						console.log('上传进度' + res.progress);
						console.log('已经上传的数据长度' + res.totalBytesSent);
						console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
					});
				},
				error: function(e) {
					console.log(e);
				}
			});
		},
		change_img(key_name) {
			var _self = this;
			// 选择图像方法
			uni.chooseImage({
				count: 1,
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album'], //从相册选择
				success: function(res) {
					const tempFilePaths = res.tempFilePaths;
					const uploadTask = uni.uploadFile({
						url: _self.$fullUrl('/api/adoption_information/upload?'),
						filePath: tempFilePaths[0],
						name: 'file',
						formData: {
							'adoption_information': 'test'
						},
						header: {
							'x-auth-token': _self.$store.state.user.token
						},
						success: function(uploadFileRes) {
							var filename = JSON.parse(uploadFileRes.data).result.url
							var img_url = filename
							_self.form[key_name] = img_url
						}
					});

					uploadTask.onProgressUpdate(function(res) {
						_self.percent = res.progress;
						console.log('上传进度' + res.progress);
						console.log('已经上传的数据长度' + res.totalBytesSent);
						console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
					});
				},
				error: function(e) {
					console.log(e);
				}
			});
		},

		/**
		 * 获取对象后获取缓存表单
		 * @param {Object} json
		 * @param {Object} func
		 */
		get_obj_before(param){
			var form = uni.db.get("form");
			if (form) {
				this.obj = uni.push(this.obj ,form);
				this.form = uni.push(this.form ,form);
			}
			var arr = []
			for (let key in form) {
				arr.push(key)
			}
			for (var i=0;i<arr.length;i++){
				this.disabledObj[arr[i] + '_isDisabled'] = true
			}
			uni.db.del("form");
			return param;
		},

		/**
		 * 获取对象后获取缓存表单
		 * @param {Object} json
		 * @param {Object} func
		 */
		get_obj_after(json ,func){
			var form = uni.db.get("form");
			var obj = Object.assign({} ,form ,this.obj);
			if (form) {
				this.obj = uni.push(this.obj ,obj);
			}
			if (form) {
				this.form = uni.push(this.form ,form);
			}
			if(func){
				func(json);
			}
		},

	},
	onLoad(){
		this.get_user_session_user();
		this.get_list_user_user();
	}
}
</script>

<style scoped>
	input{
		font-size: 10px;
	}

	.form_edit {
		background-color: #fff;
		margin-bottom: 0.5rem;
		padding: 1rem;
		font-size: 10px;
	}

	.item {
		display: flex;
		padding: 0.2rem 0;
	}

	.left_text {
		flex: 0 0 25%;
		display: flex;
		align-items: center;
	}

	.right_text {
		flex: 0 0 75%;
		border-bottom: 1px solid #eee;
	}
	.right_text.btn_warp{
		border-bottom: 0;
	}

	.btn_submit {
		text-align: center;
		background-color: #fff;
		padding: 0.3rem;
		margin: 0.1rem 1rem;
		border: 1px solid #eee;
		border-radius: 0.5rem;
	}

	.btn_submit:hover {
		opacity: 0.5;
	}
	.btn_add_img{
		color: #D3D3D3;
		text-align: center;
		border: 1px solid #eee;
		height: 5rem;
		width: 5rem;
		position: relative;
	}
	.btn_add_img text{
		font-size: 35px;
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%,-50%);
	}
</style>
